import modelEnhance from '@/utils/modelEnhance';
import PageHelper from '@/utils/pageHelper';
/**
 * 当第一次加载完页面时为true
 * 可以用这个值阻止切换页面时
 * 多次初始化数据
 */
let LOADED = false;
export default modelEnhance({
    namespace: 'adornPedia',

    state: {
        pageData: PageHelper.create(),
        dict: []
    },

    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname }) => {
                if (pathname === '/adorn_pedia' && !LOADED) {
                    LOADED = true;
                    dispatch({
                        type: 'init'
                    });
                }
            });
        }
    },

    effects: {
        // 进入页面加载
        *init({ payload }, { call, put, select }) {
            const { pageData } = yield select(state => state.adornPedia);
            yield put({
                type: 'getPageInfo',
                payload: {
                    pageData: pageData.startPage(1, 10)
                }
            });
            yield put({
                type: 'getDict'
            });
        },
        // 获取分页数据
        *getPageInfo({ payload }, { call, put }) {
            const { pageData } = payload;
            yield put({
                type: '@request',
                payload: {
                    valueField: 'pageData',
                    url: '/adorn_pedia/',
                    pageInfo: pageData,
                    method: 'get'
                }
            });
        },
        // 保存 之后查询分页
        *save({ payload }, { call, put, select, take }) {
            const { values, record, success } = payload;
            const { pageData } = yield select(state => state.adornPedia);
            // put是非阻塞的 put.resolve是阻塞型的
            yield put.resolve({
                type: '@request',
                payload: {
                    notice: true,
                    url: record ? `/adorn_pedia/${record.id}/` : '/adorn_pedia/',
                    method: record ? 'patch' : 'post',
                    data: values,
                    afterResponse: (resp) => {
                        success()
                    }
                }
            });
            yield put({
                type: 'getPageInfo',
                payload: { pageData }
            });
        },
        // 修改
        *update({ payload }, { call, put }) { },
        // 删除 之后查询分页
        *remove({ payload }, { call, put, select }) {
            const { records, success } = payload;
            const { pageData } = yield select(state => state.adornPedia);
            yield put.resolve({
                type: '@request',
                payload: {
                    notice: true,
                    url: `/adorn_pedia/${records[0].id}/`,
                    //data: records.map(item => item.rowKey),
                    method: 'delete',
                    afterResponse: (resp) => {
                        success()
                    }
                }
            });
            yield put({
                type: 'getPageInfo',
                payload: { pageData }
            });
        },
        // 获取单个数据
        *getInfoDetail({ payload }, { call, put }) {
            const { record, success } = payload;
            yield put({
                type: '@request',
                afterResponse: resp => resp.data,
                payload: {
                    valueField: 'infoDetail',
                    url: `/adorn_pedia/${record.id}/`,
                    method: 'get',
                    afterResponse: (resp) => {
                        success(resp.data)
                    }
                }
            });
        },
        // 获取数据字典
        *getDict({ payload }, { call, put }) {
            yield put({
                type: '@request',
                afterResponse: resp => resp.data,
                payload: {
                    valueField: 'dict',
                    url: '/adorn_pedia/dict/',
                    method: 'get'
                }
            });
        },
    },

    reducers: {}
});
